package com.zhiyou.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhiyou.pojo.Course;
import com.zhiyou.pojo.Subject;
import com.zhiyou.pojo.Video;
import com.zhiyou.service.CourseService;
import com.zhiyou.service.SubjectService;
import com.zhiyou.service.VideoService;
import com.zhiyou.tools.CommonTools;


@Controller
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private SubjectService subjectService;
    @RequestMapping("/course.action")
	public String showCourse(Integer subjectId,Model model){
    	System.out.println("********");
		Subject subject = subjectService.selectSubjectByPrimaryKey(subjectId);
		System.out.println("*////");
		System.out.println("-----"+subjectId);
		List<Course> courseList = courseService.selectBySubjectId(subjectId);
		System.out.println("****"+subjectId);
		for (Course course : courseList) {
			int courseId = course.getId();
			List<Video> videoList = videoService.selectVideoByCourseId(courseId);
			
			System.out.println("6666666"+"  "+courseId);
			for (Video video : videoList) {
				
				video.setShowTime(CommonTools.getShowTime(video.getTime()));
				System.out.println(video.getTime());
			}
			System.out.println("555555555");
			course.setVideoList(videoList);
			
		}
		subject.setCourseList(courseList);
		model.addAttribute("subject",subject);
		return "before/course";
	}
	@RequestMapping("/showCourseList.action")
	public String showCourseList(Model model,HttpSession session){
		
		List<Course> list=courseService.selectAll();
		model.addAttribute("list", list);
		
		
		System.out.println("0000"+list.toString());
		model.addAttribute("userName", session.getAttribute("userName"));
		return "behind/courseMgr";
	}
	@RequestMapping("/addCourse.action")
	public String addCourse(Model model){
		List<Course> list=courseService.selectAll();
		model.addAttribute("courseList", list);
		
		return "behind/addCourse";
	}
	@RequestMapping("/saveCourse.action")
	public String saveCourse(Course course) {

		courseService.saveOrUpdate(course);
		
		return "redirect:/showCourseList.action";
	}
	@RequestMapping("/courseDel.action")
	@ResponseBody  
	public String deleteById(int id) {

		int result =courseService.delById(id);
		
		if(result>0){
			return "success";
		}else{
			return "fail";
		}
	}
	@RequestMapping("/delBatchCourses.action")
	public String delBatchVideoByIds(Integer[] ids){
		
		
		courseService.delBatchByIds(ids);
		return "redirect:/showCourseList.action";
	}
	@RequestMapping("/courseEdit.action")
	public String editCourse(int id, Model model) {

		
		Course course = courseService.selectById(id);
		model.addAttribute("course", course);
		
		List<Course> courseList = courseService.selectAll();
				
		model.addAttribute("courseList", courseList);
		
		
		

		return "behind/addCourse";
	}
}
